package com.umfb.bootstrap;

import com.umfb.bootstrap.config.ManagerSiteMeshFilter;
import com.umfb.bootstrap.intercepter.LoginInterceptor;
import com.umfb.bootstrap.intercepter.ParamsValidateInterceptor;
import com.umfb.config.zk.ZooKeeperPropertyPlaceholderConfigurer;
import com.umfb.utils.UmfbApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.Ordered;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.text.SimpleDateFormat;
import java.util.Date;

@SpringBootApplication
@EnableAutoConfiguration
public class DubboDemoBootstrapApplication extends WebMvcConfigurerAdapter {
	public  static  final Logger logger = LoggerFactory.getLogger(DubboDemoBootstrapApplication.class);
	public static void main(String[] args) {
		SpringApplication.run(DubboDemoBootstrapApplication.class, args);

		final ApplicationContext ac = SpringApplication.run(DubboDemoBootstrapApplication.class, args);
		UmfbApplicationContext.setApplicationContext(ac);
	}

	@Bean
	public UmfbApplicationContext initAppEnd(ApplicationContext ac) {
		UmfbApplicationContext.setApplicationContext(ac);
		UmfbApplicationContext app = new UmfbApplicationContext();
		logger.warn("demo war started");
		return app;

	}

	@Bean
	public FilterRegistrationBean siteMeshFilter() {
		FilterRegistrationBean filter = new FilterRegistrationBean();
		filter.setFilter(new ManagerSiteMeshFilter());
		return filter;
	}
	private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
	@Bean
	public PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
		PropertySourcesPlaceholderConfigurer configurer = new ZooKeeperPropertyPlaceholderConfigurer();
		configurer.setIgnoreResourceNotFound(true);
		configurer.setLocations(resourcePatternResolver.getResource("classpath:application.properties"));
		return configurer;
	}
	@Bean
	public HandlerInterceptor paramsValidateInterceptor(){
		return new ParamsValidateInterceptor();
	}

	public HandlerInterceptor loginInterceptor(){
		return  new LoginInterceptor();
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//注册拦截器
		logger.info("Interceptor start"+"["+new SimpleDateFormat("HH:ss:mm").format(new Date())+"]");
		registry.addInterceptor(paramsValidateInterceptor()).addPathPatterns("/**");
		registry.addInterceptor(loginInterceptor()).addPathPatterns("/**");
		logger.info("Interceptor finish"+"["+new SimpleDateFormat("HH:ss:mm").format(new Date())+"]");
		super.addInterceptors(registry);
	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("redirect:/login");
		registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
		super.addViewControllers(registry);
	}
}
